package encaps.action;

import encaps.entities.Grant;
import encaps.entities.Role;
import encaps.logging.JMSUtil;
import encaps.logging.Log;
import encaps.resources.Image;
import encaps.resources.Labels;
import encaps.resources.Response;
import encaps.security.SecuritySystem;
import encaps.utils.*;
import java.sql.SQLException;
import java.util.Date;
import javax.ejb.FinderException;
import javax.naming.NamingException;
import org.jdom.Element;

public class EditRoleAction extends Action {

    public static final String EDIT_ROLE = "editRole";
    public static final String ROLE = "role";
    public static final String NAME = "name";
    public static final String GRANT = "grant";
    public static final String GRANTS = "grants";
    protected ActionContext actionContext;
    private Long id;

    public EditRoleAction(ActionContext context, Grant requiredGrant) {
        this.actionContext = context;
        this.requiredGrant = requiredGrant;
    }

    @Override
    public Element execute() throws ActionExecutionException {
        try {
            String urole = actionContext.getTextParameter(ID);
            Role role = SecuritySystem.getRole(urole);
            Element response = XMLHelper.createElement(Response.RESPONSE);
            Element e = XMLHelper.addChild(response, Response.ENTITY);
            e.setAttribute(Response.EDIT, "true");
            //String s="";
            String name, grants;
            name = role.getRoleName();
            grants = role.getGrantsAsString();

            XMLHelper.addChild(e, XMLHelper.createHeader(String.format(Labels.getLabel(Labels.EDIT_ROLE), name)));
            XMLHelper.addChild(e, XMLHelper.createTextField(Labels.getLabel(Labels.ROLE_NAME), name, NAME));
            //XMLHelper.addChild(e, XMLHelper.createTextArea(Labels.getLabel(Labels.GRANTS), grants, GRANT));


            XMLHelper.addChild(e, XMLHelper.createSmallHeader(Labels.getLabel(Labels.GRANTS)));
            XMLHelper.addChild(e, XMLHelper.createField(GRANTS, role.getGrantsAsString(), role.getGrantsAsString(), Response.MULTISELECT));
            XMLHelper.addChild(e, XMLHelper.createHiddenTextField(role.getGrantsAsString(), GRANTS));

            XMLHelper.addChild(e, XMLHelper.createField(Labels.getLabel(Labels.MOVIE_PERSON_REMBUTTON), "f_optionDelete(['"
                    + GRANTS + "'])", "noname", Response.JSBUTTON));
            XMLHelper.addChild(e, XMLHelper.createLineBreak());
            XMLHelper.addChild(e, XMLHelper.createField(Labels.getLabel(Labels.ADD_GRANT), "f_optionMove('all','grants')", "name", Response.JSBUTTON));
            XMLHelper.addChild(e, XMLHelper.createLineBreak());
            try {
                String othergrants = Provider.getUniqueString(RoleGrantGenreEnum.GRANT);
                String[] exigrants = role.getGrantsAsString().split(";");
                for (String buf : exigrants) {
                    othergrants.replaceAll(buf + ";", "");
                }
                XMLHelper.addChild(e, XMLHelper.createField("all", othergrants, othergrants, Response.MULTISELECT));
            } catch (SQLException ex) {
                throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
            }
            XMLHelper.addChild(e, XMLHelper.createHiddenTextField(name, ID));
            XMLHelper.addChild(e, XMLHelper.createLineBreak());
            XMLHelper.addChild(e, XMLHelper.createLineBreak());
            XMLHelper.addChild(e, XMLHelper.createImageButton(Image.getImage(Image.APPLY), ApplyRoleChangesAction.APPLY_ROLE_CHANGES));
            JMSUtil.sendLogMessage(Log.UPDATE,
                    EntityUtil.getUserHome().findByLogin(actionContext.getUserLogin()).getID(),
                    actionContext.getUserLogin(), new Date(), Role.TABLE_NAME,
                    null, name);
            return response;
        } catch (NamingException ex) {
            throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
        } catch (FinderException ex) {
            throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
        } catch (DataAccessException ex) {
            throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
        } catch (SystemException ex) {
            throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
        }
    }
}
